from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import datetime
import os
import sys
from selenium.webdriver.support import expected_conditions as EC

class Driver:
    driver = ""

    def __init__(self):
        options = webdriver.ChromeOptions()
        options.add_argument('--start-maximized') # 浏览器最大化
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) 
        self.driver.implicitly_wait(2)
    
     # 创建屏幕截图（利用selenium提供的截屏方法）
    def getScreenShot(self):
        # 目录: ../images/2025/3/20/  自动创建当天日期的文件夹,方便查找
        # 文件名： 调用方法名-2025-03-19-120530.png  (年-月-日-时分秒)
        dirname = datetime.datetime.now().strftime("%y-%m-%d")  # 2025-03-20

        # 判断是否已经存在文件夹，不存在即创建文件夹
        if not os.path.exists("../images/" + dirname):
            os.mkdir("../images/" + dirname)
        # 得到调用当前函数的函数名，即上一层栈帧
        filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime("%H_%M_%S") + ".png"

        # ../images/2025-03-20/ForumLogin-12_20_06
        self.driver.save_screenshot("../images/" + dirname +"/" + filename)


ForumDriver=Driver()
